package com.iot.ic.common.utils;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.xml.sax.SAXException;


/**
 * 资源工具类 Created by Administrator on 2015/8/28 16:17. Author: Songxy Email:
 * thinkdata@163.com
 */
public class ResourceUtil extends PropertyPlaceholderConfigurer implements
		ApplicationContextAware {

	final static String					TAG				= "ResourceUtil";
	public static final String[]		PICTURE_SUFFIX	= { "png", "jpg", "jpeg" };
	private static Map<String, Object>	propsMap;
	private static ApplicationContext applicationContext;

	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
		super.processProperties(beanFactory, props);

		propsMap = new HashMap<String, Object>();
		for (Map.Entry<Object, Object> entry : props.entrySet()) {
			String key = entry.getKey().toString();
			Object obj = entry.getValue();
			propsMap.put(key, obj);
		}

	}

	public static Object getContextProps(String name) {
		try {
			return propsMap.get(name);
		} catch (NullPointerException e) {
			return null;
		}
	}

	public static File getPictureFile() {
		URI path = URI.create((String) getContextProps("file.path"));
		File file1 = new File(path);
		if (!file1.exists()) {
			file1.mkdirs();
		}
		return file1;
	}

	public static Map<String, String> getLayoutMappingsResource(ApplicationContext context, int resourceId) throws IOException {
		Map<String, String> result = null;
		String key = null, value = null;
		// try {
		// XmlResourceParser parser = checkXml(context, resourceId);
		// int eventType = parser.getEventType();
		// while (eventType != XmlResourceParser.END_DOCUMENT) {
		// if (eventType == XmlResourceParser.START_DOCUMENT) {
		// Log.d(TAG, "Start Document");
		// } else if (eventType == XmlResourceParser.END_TAG) {
		// if (parser.getName().equals("entry")) {
		// result.put(key, value);
		// key = null;
		// value = null;
		// }
		// } else if (eventType == XmlResourceParser.START_TAG) {
		// if (parser.getName().equals("map")) {
		// boolean isLinked =
		// Boolean.parseBoolean(parser.getAttributeValue(null, "linked"));
		// result = isLinked ? new LinkedHashMap() : new HashMap();
		// } else if (parser.getName().equals("entry")) {
		// key =
		// context.getResources().getString(context.getResources().getIdentifier(
		// parser.getAttributeValue(0), "string", context.getPackageName()));
		//
		// }
		// } else if (eventType == XmlResourceParser.TEXT) {
		// if (key != null) {
		// value = parser.getText();
		// }
		// }
		// eventType = parser.next();
		// }
		// } catch (Resources.NotFoundException e) {
		// throw new IOException("Resource is Not Found");
		// } catch (XmlPullParserException e) {
		// throw new IOException("Resource Xml Pull Parser Error");
		// }
		return result;
	}


	// private static XmlResourceParser checkXml(Context context, int
	// resourceId)
	// throws IOException {
	// XmlResourceParser parser = null;
	// parser = context.getResources().getXml(resourceId);
	// if (parser == null) throw new IOException("File Not Found");
	// return parser;
	// }
	
	public static <T> T getBean(Class<T> classType) {
        return applicationContext.getBean(classType);  
    } 
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}
}
